package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.User;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.UserService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author rainbow
 **/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    /**
     * 新增一个用户
     *
     * @param user
     * @return
     */
    @Override
    public Map<String, Object> add(User user) {
        Map<String, Object> result = new HashMap<>();
        // 调用保存的方法（继承ServiceImpl<UserMapper, User>，自动有很多方法）
        // 或者使用Mapper的insert方法：baseMapper.insert(user);
        save(user);
        result.put("code", 0);
        result.put("success", true);
        result.put("data", user);

        return result;
    }

    /**
     * 根据id获取用户信息
     */
    @Override
    public Map<String, Object> getInfo(Long id) {
        User user = baseMapper.selectById(id);
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("success", true);
        result.put("data", user);
        return result;
    }

    /**
     * 根据id修改用户信息
     */
    @Override
    public Map<String, Object> update(User user) {
        user.setUsername("修改后的用户名");
        updateById(user);

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("success", true);
        result.put("data", user);
        return result;
    }

    /**
     * 获取用户列表
     */
    @Override
    public Map<String, Object> list() {
        // 可增加查询条件
        List<User> list = list(new QueryWrapper<>());

        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("success", true);
        result.put("data", list);
        return result;
    }
}
